package com.sdhs.paas.itims.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.itims.bean.AppInfoBean;
import com.sdhs.paas.itims.dto.AppInfoDTO;
import com.sdhs.paas.itims.dto.req.AppInfoReqDTO;
import com.sdhs.paas.itims.dto.res.AppInfoResDTO;
import com.sdhs.paas.itims.service.AppInfoService;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.page.Page;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;

/**
 * @author AppInfo
 * @version 1.0
 * @Title:  代码生成接口，用于接受前端传输过来的数据
 * @author kangjichang
 * @date 2018-09-18
 */
 
@RefreshableRestController
@RequestMapping(value = "/appInfo")
public class AppInfoController {

    @Autowired
    private AppInfoService appInfoService;
    
    /**
     * 分页查询接口
     * */
    @PostMapping("/getAppInfoPageList")
	public PaasBaseResponse<AppInfoResDTO> getAppInfoPageList(@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
    	AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
    	PageInfoDTO pageInfoDTO = appInfoReqDTO.getBody().getPageInfo();
    	
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
    	
		AppInfoBean itimsBusinessBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);
    	Page<AppInfoBean> page = appInfoService.getPageAppInfoList(pageRequest, itimsBusinessBean);
    	PageInfoDTO resPageInfoDTO = new PageInfoDTO();
    	AppInfoResDTO appInfoResDTO = new AppInfoResDTO();
    	resPageInfoDTO.setPage(page.getCurPage());
		resPageInfoDTO.setPageSize(page.getPageSize());
		resPageInfoDTO.setPageCount(page.getPageCount());
		resPageInfoDTO.setRowCount(page.getTotalCount());
		appInfoResDTO.setPageInfo(resPageInfoDTO);
		List<AppInfoBean> appInfoBeanList = page.getResult();
		List<AppInfoDTO> dtoList = new ArrayList<AppInfoDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(appInfoBeanList, new TypeReference<List<AppInfoDTO>>() {
		});
		appInfoResDTO.setAppInfoDTOList(dtoList);
    	return new PaasBaseResponse<>(appInfoResDTO);
	}
	
	/**
	 * 保存接口
	 * */
	@PostMapping("/saveAppInfo")
	public PaasBaseResponse<AppInfoResDTO> saveAppInfo(@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
		AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
		AppInfoBean appInfoBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		appInfoBean.setId(uuid);
		appInfoBean.setIsDeleted(0);
		Boolean flag = appInfoService.saveAppInfo(appInfoBean);
		appInfoDTO.setId(uuid);
		AppInfoResDTO appInfoResDTO = new AppInfoResDTO();
		appInfoResDTO.setAppInfoDTO(appInfoDTO);
		return new PaasBaseResponse<>(appInfoResDTO);
	}
	
	/**
	 * 修改接口
	 * */
	@PostMapping("/updateAppInfo")
	public PaasBaseResponse<AppInfoResDTO> updateAppInfo(@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
		AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
		AppInfoBean appInfoBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);
		Boolean flag = appInfoService.updateAppInfo(appInfoBean);
		AppInfoResDTO appInfoResDTO = new AppInfoResDTO();
		appInfoResDTO.setAppInfoDTO(appInfoDTO);
		return new PaasBaseResponse<>(appInfoResDTO);
	}
	
	/**
	 * 获取详情接口
	 * */
	@PostMapping("/getAppInfoDetail")
	public PaasBaseResponse<AppInfoResDTO> getAppInfoDetail(@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
		AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
		AppInfoBean appInfoBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);
		appInfoBean = appInfoService.getAppInfoDetail(appInfoBean.getId());
		appInfoDTO = EntityDtoConvertUtil.convertToDTO(appInfoBean, AppInfoDTO.class);
		AppInfoResDTO appInfoResDTO = new AppInfoResDTO();
		appInfoResDTO.setAppInfoDTO(appInfoDTO);
		return new PaasBaseResponse<>(appInfoResDTO);
	}
	
	/**
	 * 删除接口
	 * */
	@PostMapping("/deleteAppInfo")
	public PaasBaseResponse<AppInfoResDTO> deleteAppInfo(@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
		AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
		AppInfoBean appInfoBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);
		Boolean flag = appInfoService.deleteAppInfo(appInfoBean);
		AppInfoResDTO appInfoResDTO = new AppInfoResDTO();
		return new PaasBaseResponse<>(appInfoResDTO);
	}
}
